package com.zhangxin.threadcoreknowledge.stopthread;

public class Demo06RightWayStopThreadinPord implements Runnable{

    // 在方签名中抛出异常，那么在run 中就会强制try catch
    private void throwInMethod() throws InterruptedException{
        Thread.sleep(2000);
    }

    @Override
    public void run() {
        while (true && !Thread.currentThread().isInterrupted()) {
            System.out.println("go");
            try {
                throwInMethod();
            } catch (InterruptedException e) {
                Thread.currentThread().isInterrupted();
                // 保存日志，停止程序
                System.out.println("保存日志");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Demo06RightWayStopThreadinPord());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }

}
